//
//  WCG_N_ViewController.m
//  Project_B
//
//  Created by 黑黑 on 15/11/24.
//  Copyright © 2015年 Atom_Wang. All rights reserved.
//

#import "WCG_N_ViewController.h" // 第一页表视图
#import "WCG_N_TableViewCell.h"  // cell
#import "GXProviderDataModel.h"  // 数据模型
#import "WCG_N_ProviderDetailViewController.h" // 第二页视图
#import "GXiaNetHandle.h"        // 网络请求
#import "WebsIteManager.h"       // 网址管理

//造物者 cell 重用标识
static NSString *providerCellId = @"providerCell";

@interface WCG_N_ViewController () <UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, retain) NSMutableArray *providerDataArr; // 数据数组
@end

@implementation WCG_N_ViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar setHidden:YES];
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 王霄写~
    UIView *temp = self.tabBarController.view.subviews[2];
    [UIView animateWithDuration:1 animations:^{
        temp.frame = CGRectMake(0, kScreenHeight - 49, kScreenWidth, 49);
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    
    /* 初始化数据数组 */
    self.providerDataArr = [NSMutableArray array];

    /* 创建表视图 */
    [self createTableViewWithFrame:CGRectMake(0, 0, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.height - 49)];
    self.tableView.dataSource = self;
    self.tableView.delegate = self;

    /* 注册cell */
    [self.tableView registerClass:[WCG_N_TableViewCell class] forCellReuseIdentifier:providerCellId];
    
    /* 请求数据 */
    [self getProviderData];
    
    /* 小菊花 */
    [self loadNightMBProgress];
}

#pragma mark >>> 数据请求方法
- (void)getProviderData {
        [GXiaNetHandle getUrl:kProviderWeb cookie:nil success:^(id result) {
            [self removeHud]; //
            NSArray *arr = result[@"providers"];
            //`字典转化模型
            self.providerDataArr = [GXProviderDataModel transformWithArray:arr];
            if (self.providerDataArr != 0) {
                [self.tableView reloadData];
            }
            
        } failure:^(NSError *error) {
            NSLog(@"%@", error);
        }];
}

#pragma mark >>> 行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (self.providerDataArr.count != 0) {
        return self.providerDataArr.count;
    } else {
        return 0;
    }
}

#pragma mark >>> cell的内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    WCG_N_TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:providerCellId];
    
    // 判断加载出的内容是否大于表视图显示的内容
    if (self.providerDataArr.count > indexPath.row) {
        cell.providerModel = self.providerDataArr[indexPath.row];
        return cell;
    } else {
        return nil;
    }  
}

#pragma mark >>> 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 115;
}

#pragma  mark >>> cell的点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WCG_N_ProviderDetailViewController *providerDetailVC = [[WCG_N_ProviderDetailViewController alloc] init];
    
    providerDetailVC.provider_id = [self.providerDataArr[indexPath.row] provider_id];
    [self.tabBarController.tabBar setHidden:YES];
    [self.navigationController pushViewController:providerDetailVC animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
